/*
 * @lc app=leetcode.cn id=1760 lang=javascript
 *
 * [1760] 袋子里最少数目的球
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} maxOperations
 * @return {number}
 */

/**
 * 最小开销结余1到最大值
 * 二分 1-最大值
 */

// 计算操作次数
function getN(nums, x){
  let count = 0;

  for (let num of nums) { 
    count += Math.floor((num - 1) / x); 
  }
  return count;
}

function bs(nums, l, r, n){
  if(l == r) return l;
  const mid = l + ((r - l)>>1); // 最大开销
  if(getN(nums, mid) > n){ 
    l = mid + 1;
  } else {
    r = mid;
  }
  return bs(nums, l, r, n);
}

var minimumSize = function(nums, maxOperations) {
  let l = 1, r = 1;
  for (const num of nums) r = Math.max(num, r);
  return bs(nums, l, r, maxOperations);
};
// @lc code=end

